package io.github.fan0424.composedemo1

import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.flow.flow
import java.util.concurrent.Flow

/**
 *
 * Created by fan.feng on 2021/9/6.
 */
class AndroidWithComposeActivity: ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        //android view中设置compose
//        setContentView(R.layout.activity_android_with_compose)
//        val composeView = findViewById<ComposeView>(R.id.composeView)
//        composeView.setContent {
//            Text(text = "我是compose")
//        }

        val numLiveData = MutableLiveData<Int>(1)
        val flow = flow<Int> {}

        //compose中设置android view
        setContent {
            //在compose使用需要转换一下，livedata需要引入库：androidx.compose.runtime:runtime-livedata:$compose_version
            val numInCompose by numLiveData.observeAsState()
            val flowInCompose = flow.collectAsState(initial = 1)

            Column {
                Text(text = "我是compose$numInCompose")
                AndroidView({
                    //这里是android原生view初始代码，【只会执行一次】
                    TextView(this@AndroidWithComposeActivity).apply {
                        setBackgroundColor(Color.GREEN)
                        text = "我是android view"
                    }
                }, Modifier.fillMaxWidth()){
                    //这里是recompose过程中执行的代码, 推荐更新代码写到这【会执行多次】
                }
                Text(text = "我还是Compose$flowInCompose")

            }
        }
    }

}